import os
import numpy as np
from matplotlib import pyplot as plt
import cv2

filename = 'D:\Code\python\JupterNotebook\W0\lena.jpg'
bgr_img = cv2.imread(filename)
cv2.imshow('bgr_img', bgr_img)

print('======RGB操作======')
rgb_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB)
cv2.imshow('rgb_img', rgb_img)
R, G, B = cv2.split(bgr_img)  # 分离出图片的B，R，G颜色通道
cv2.imshow('r', R)  # 实际是 [R,R,R]组成的图片，所以会显示为灰色图
cv2.imshow('g', G)  # 实际是 [G,G,G]组成的图片，所以会显示为灰色图
cv2.imshow('b', B)  # 实际是 [B,B,B]组成的图片，所以会显示为灰色图

image = cv2.merge([R, G, B])
cv2.imshow('image', image)

zeros = np.zeros(rgb_img.shape[:2], dtype="uint8")

r_m = cv2.merge([R, zeros, zeros])
cv2.imshow('r_m', r_m)

g_m = cv2.merge([zeros, G, zeros])
cv2.imshow('g_m', g_m)

b_m = cv2.merge([zeros, zeros, B])
cv2.imshow('b_m', b_m)

print(np.shape(R))
print('======HSV操作======')

hsv_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2HSV)
cv2.imshow('hsv_img', hsv_img)

H, S, V = cv2.split(hsv_img)  # 分离出图片的H，S，V通道
image2 = cv2.merge([H, S, V])

cv2.imshow('H', H)
cv2.imshow('S', S)
cv2.imshow('V', V)
cv2.imshow('Image_Merged', image2)

cv2.waitKey(0)
